package info.zhou.tank;

import robocode.AdvancedRobot;
import robocode.ScannedRobotEvent;

import java.awt.*;

/**
 * Author: zhouinfo
 * Create Date：2016-05-10 20:22
 */
public class TankRobot extends AdvancedRobot {

    Enemy enemy = new Enemy();
    public static double PI = Math.PI;

    @Override
    public void run() {
        setAdjustGunForRobotTurn(true);
        setAdjustRadarForGunTurn(true);
        setAdjustRadarForRobotTurn(true);

        setBodyColor(Color.black);
        setGunColor(Color.black);
        setRadarColor(Color.orange);
        setBulletColor(Color.cyan);
        setScanColor(Color.cyan);
        while (true) {
            if (enemy.getName() == null) {
                setTurnRadarRightRadians(2 * PI);
                execute();
            } else {
                execute();
            }
        }
    }

    @Override
    public void onScannedRobot(ScannedRobotEvent event) {
        enemy.update(event, this);
        double Offset = rectify(enemy.getDirection() - getRadarHeadingRadians());
        setTurnRadarRightRadians(Offset * 1.5);
        setAhead(50);
        setTurnRight(90);
        setTurnGunLeftRadians(Offset);
        fire(3);
    }

    //角度修正方法，重要
    public double rectify(double angle) {
        if (angle < -Math.PI)
            angle += 2 * Math.PI;
        if (angle > Math.PI)
            angle -= 2 * Math.PI;
        return angle;
    }
}
